package com.hmyd.hmyd_android.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TableLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hmyd.hmyd_android.R;
import com.hmyd.hmyd_android.AppBasic;
import com.hmyd.hmyd_android.commons.UrlConstant;
import com.hmyd.hmyd_android.dto.Base;
import com.hmyd.hmyd_android.dto.WithdrawDetail;
import com.hmyd.hmyd_android.dto.WithdrawFee;
import com.hmyd.hmyd_android.utils.MyToast;
import com.hmyd.hmyd_android.utils.Utils;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;

@ContentView(R.layout.activity_confirm_withdrawals)
public class WithdrawalsDetailActivity extends BasicActivity implements
		View.OnClickListener {
	@ViewInject(R.id.home_title)
	public View home_title;
	@ViewInject(R.id.home_title_txt)
	public TextView home_title_txt;
	@ViewInject(R.id.home_right)
	public TextView home_right;

	private final int DODEPOSIT = 2;
	
	private static String SER_KEY = "SER_KEY";
	
	@ViewInject(R.id.withdrawAmount)
	private TextView withdrawAmount;
	@ViewInject(R.id.realWithdrawAmount)
	private TextView realWithdrawAmount;
	@ViewInject(R.id.managementFee)
	private TextView managementFee;
	@ViewInject(R.id.sinaAmount)
	private TextView sinaAmount;
	
	@ViewInject(R.id.password)
	private EditText password;
	@ViewInject(R.id.submit)
	private Button submit;
	
	private WithdrawDetail withdrawDetail;
	private String bindingPayCardId;
	private String availableDou;
	
	public static Intent getIntent(Context context,WithdrawDetail withdrawDetail,String bindingPayCardId,String availableDou){
		Intent intent = new Intent(context,WithdrawalsDetailActivity.class);
		Bundle mBundle = new Bundle();  
        mBundle.putSerializable(SER_KEY,withdrawDetail);
		intent.putExtras(mBundle);
		intent.putExtra("bindingPayCardId", bindingPayCardId);
		intent.putExtra("availableDou", availableDou);
		
		return intent;
	}
	
	private TextWatcher watcher = new TextWatcher() {
        
        @Override    
        public void afterTextChanged(Editable s) {
        }   
          
        @Override 
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }  
 
         @Override    
        public void onTextChanged(CharSequence s, int start, int before, int count) {
        	 if(TextUtils.isEmpty(s)){
        		 submit.setEnabled(false);
        	 }else{
        		 submit.setEnabled(true);
        	 }
        }                    
    };
    
	public void onPostSuccess(int postId, String result) {
		Gson gson = new Gson();
		switch (postId) {
		case DODEPOSIT:
			
			java.lang.reflect.Type type3 = new TypeToken<Base>() {}.getType();
			Base base = gson.fromJson(result, type3);
			MyToast.showHMToast(this, true, base.info);
			
			AppBasic.getInstance().finishCurrentActivity();
			break;
		default:
			break;
		}
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.submit:
			RequestParams params = new RequestParams();
			params.addBodyParameter("withdrawId", withdrawDetail.withdrawId);
			params.addBodyParameter("bindingPayCardId", bindingPayCardId);
			params.addBodyParameter("tradePassword", password.getText().toString());
			params.addBodyParameter("availableDou", availableDou);
			params.addBodyParameter("realWithdrawDou", withdrawDetail.realWithdrawAmount+"");
			params.addBodyParameter("managementFeeDou", withdrawDetail.managementFee+"");
			params.addBodyParameter("newDepositDou", withdrawDetail.newDepositAmount+"");
			
			onRequest(DODEPOSIT, UrlConstant.DOWITHDRAW, HttpMethod.POST, params);
			
			break;
		default:
			break;
		}
	}

	@Override
	protected void setParams() {
		home_title_txt.setText(getString(R.string.confirm_withdrawals));
		home_title.setOnTouchListener(touch);
		
		withdrawDetail = (WithdrawDetail)getIntent().getSerializableExtra(SER_KEY);
		bindingPayCardId = getIntent().getStringExtra("bindingPayCardId");
		availableDou = getIntent().getStringExtra("availableDou");
		
		/*LayoutInflater mInflater = LayoutInflater.from(this);
		
		if(withdrawDetail.list!=null && withdrawDetail.list.size()>0){
			for(WithdrawFee withdrawFee:withdrawDetail.list){
				View view_withdrawals_fee = mInflater.inflate(R.layout.view_withdrawals_fee, null);
				TextView name = (TextView)view_withdrawals_fee.findViewById(R.id.name);
				TextView capitalAmount = (TextView)view_withdrawals_fee.findViewById(R.id.capitalAmount);
				TextView managementFee = (TextView)view_withdrawals_fee.findViewById(R.id.managementFee);
			
				name.setText(withdrawFee.name);
				capitalAmount.setText(Utils.amountConversionFormat(withdrawFee.capitalAmount)+"元");
				managementFee.setText(Utils.amountConversionFormat(withdrawFee.managementFee)+"元");
			}
		}*/
		
		withdrawAmount.setText(Utils.amountConversionFormat(withdrawDetail.list.get(0).capitalAmount)+"元");
		realWithdrawAmount.setText(Utils.amountConversionFormat(withdrawDetail.realWithdrawAmount));
		managementFee.setText(Utils.amountConversionFormat(withdrawDetail.managementFee)+"元");
		sinaAmount.setText(Utils.amountConversionFormat(withdrawDetail.sinaAmount)+"元");
		
		password.addTextChangedListener(watcher);
		submit.setOnClickListener(this);
	}

	@Override
	protected void leftButton() {
		AppBasic.getInstance().finishCurrentActivity();
	}

	@Override
	protected void rightButton() {
		
	}

	@Override
	public void onPostFailure(int postId, String msg) {
		
	}

}
